<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户授权</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container" id="app">
    <div class="layuimini-main">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>用户授权</legend>
        </fieldset>
        <form class="layui-form" action="">
            <input type="hidden" name="id" :value="detail.id">
            <div class="layui-form-item">
                <label class="layui-form-label">用户名:</label>
                <div class="layui-input-inline">
                    <div class="layui-form-mid">
                        {{detail.username}}
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">授权:</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="role_ids[]" :value="item.id" :title="item.name" v-for="item in roles" :checked="selfRoleIds.indexOf(item.id)!==-1"/>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">授权数据组:</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="groups_ids[]" :value="item.id" :title="item.name" v-for="item in groups" :checked="selfGroupIds.indexOf(item.id)!==-1"/>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="submit">保存</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="/js/common.js?v=5" charset="utf-8"></script>
<script>
Id = urlParam('id')
layui.use(['form','jquery'], function () {
    var form = layui.form
        , layer = layui.layer
        ,$ = layui.jquery;

    new Vue({
        el:"#app",
        data:{
            detail:{
                id:null,
                username:null,
                password:null,
            },
            selfRoleIds:[],
            selfGroupIds:[],
            roles:[],
            groups:[],
        },
        methods:{
            formRander:function(){
                layui.use(['form'],function(){
                    layui.form.render();
                })
            },
        },
        mounted:function(){
            var _this = this;
            //监听提交
            $.get('/admin/role/paginate',{},function(res){
                _this.roles = res.data;
            });
            $.get('/admin/serverGroup/paginate',{},function(res){
                _this.groups = res.data;
            });
            //获取详情
            $.get('/admin/admin/'+Id,{},function(res){
                _this.detail = res.data
                var selfRoleIds = []
                for(var k in res.data.roles){
                    selfRoleIds.push(res.data.roles[k].id)
                }
                var selfGroupIds = []
                for(var k in res.data.groups){
                    selfGroupIds.push(res.data.groups[k].id)
                }
                _this.selfRoleIds = selfRoleIds
                _this.selfGroupIds = selfGroupIds

            });

            form.on('submit(submit)', function (data) {
                var formData = data.field;
                formData._method = 'put';
                $.post('/admin/admin/'+Id+'/auth',formData,function(res){
                    if (res.code===0){
                        layer.msg('success',{icon:6,time:500},function(){
                            parent.location.reload();//父页面刷新
                        })
                    } else {
                        layer.msg(res.msg,{icon:5})
                    }
                })
                return false;
            });
        },
        updated:function(){
            this.formRander();
        }
    })
})
</script>

</body>
</html>